package com.zbkj.client.controller;

import com.zbkj.client.utils.SmsUtil;
import com.zbkj.common.request.ClientLoginRequest;
import com.zbkj.common.request.ClientSendCodeRequest;
import com.zbkj.common.request.ClientUpdatePasswordRequest;
import com.zbkj.common.response.ClientLoginResponse;
import com.zbkj.common.response.SystemLoginResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.client.service.ClientLoginService;
import com.zbkj.client.config.ClientTokenComponent;
import com.zbkj.common.utils.CrmebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 客户端登录控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/client")
@Api(tags = "客户端登录控制器")
public class ClientLoginController {

    @Autowired
    private ClientLoginService clientLoginService;

    @Resource
    private ClientTokenComponent tokenComponent;

    @Autowired
    private SmsUtil smsUtil;


    @ApiOperation(value = "发送验证码")
    @RequestMapping(value = "/sendCode", method = RequestMethod.POST)
    public CommonResult<Boolean> sendCode(@RequestBody @Validated ClientSendCodeRequest request) {
        Boolean result = clientLoginService.sendCode(request);
//        boolean result = smsUtil.sendCode(request.getPhone(), "123456");

        return CommonResult.success(result);
    }

    @ApiOperation(value = "客户端登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult<ClientLoginResponse> login(@RequestBody @Validated ClientLoginRequest loginRequest, HttpServletRequest request) {

        String ip = CrmebUtil.getClientIp(request);

        ClientLoginResponse response = clientLoginService.login(loginRequest, ip);

        //判断套餐过期
        Date currentDate = new Date(); // 当前时间
        if(currentDate.after(response.getExpiredTime())){
            // 套餐过期时间判断
            return CommonResult.result(10012, "您的套餐已经到期", null);
        }

        return CommonResult.success(response);
    }

    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public CommonResult<String> updatePassword(@RequestBody @Validated ClientUpdatePasswordRequest request, HttpServletRequest httpRequest) {
        // 获取当前登录用户ID
        Integer userId = tokenComponent.getLoginUser(httpRequest);
        if (userId == null) {
            return CommonResult.failed("用户未登录");
        }
        
        Boolean result = clientLoginService.updatePassword(request, userId);
        if (result) {
            return CommonResult.success("密码修改成功");
        }
        return CommonResult.failed("密码修改失败");
    }
} 